package com.example.myapplication;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.adapter.DynastyPagerAdapter;
import com.example.myapplication.frament.dynasty.Clothes;
import com.example.myapplication.frament.dynasty.Emperror;
import com.example.myapplication.frament.dynasty.Introduction;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class DynastyDialog extends DialogFragment {

    private List<Fragment> list;
    private View view;
    private int dialogHeight = 0;
    private int dialogWidth;
    double initY = 0, finalY = 0;
    private static String content = "";
    private VelocityTracker velocityTracker;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCancelable(false);
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.popwin_anim_style;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        dialogWidth = (displayMetrics.widthPixels);
        params.width = dialogWidth;
        if (dialogHeight == 0) {
            dialogHeight = (displayMetrics.heightPixels) * 4 / 5;
        }
        params.height = dialogHeight;
        window.setAttributes(params);
    }

    public static String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void changeDialog(int height) {
        getDialog().setCancelable(false);
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.popwin_anim_style;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        dialogWidth = (displayMetrics.widthPixels);
        params.width = dialogWidth;
        params.height = height;
        window.setAttributes(params);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int height;
        view = View.inflate(getContext(), R.layout.dynasty_dialog, null);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewPager viewPager = view.findViewById(R.id.dynasty_viewpager);
        TabLayout tabLayout = view.findViewById(R.id.dynasty_tab);
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        list = new ArrayList<>();
        list.add(new Introduction());
        list.add(new Clothes());
        list.add(new Emperror());
        DynastyPagerAdapter adapter = new DynastyPagerAdapter(getChildFragmentManager(), list);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("简介");
        tabLayout.getTabAt(1).setText("服饰");
        tabLayout.getTabAt(2).setText("君主");

        tabLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initY = event.getY();
                        System.out.println("initY = " + initY);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        finalY = event.getY();
                        System.out.println("finalY = " + finalY);
                        break;
                    case MotionEvent.ACTION_UP:
                        if ((initY - finalY) < 0){
                            dismiss();
                        }else if ((initY - finalY) > 0){
                            DisplayMetrics displayMetrics = new DisplayMetrics();
                            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                            changeDialog((int)(dialogHeight+Math.abs(initY-finalY)));
                        }
                        break;
                }
                return true;
            }
        });
    }
}